package main

import (
	"encoding/hex"
	"fmt"

	libconsensus "github.com/tokentransfer/interfaces/consensus"
	libcore "github.com/tokentransfer/interfaces/core"
	libp2p "github.com/tokentransfer/interfaces/p2p"

	"github.com/tokentransfer/node/account"
	"github.com/tokentransfer/node/block"
	"github.com/tokentransfer/node/config"
	"github.com/tokentransfer/node/consensus"

	"github.com/tokentransfer/consensus/p2p"
	"github.com/tokentransfer/consensus/test"
)

func testConsensus(config *config.Config) (*consensus.Node, libconsensus.ConsensusService, libp2p.P2PService, libcore.Address) {
	as := account.GetAccountService()
	_, nodeKey, err := as.NewKeyFromSecret(config.GetSecret())
	if err != nil {
		panic(err)
	}
	nodeAccount, err := nodeKey.GetAddress()
	if err != nil {
		panic(err)
	}

	n := consensus.NewNode()
	err = n.Init(config)
	if err != nil {
		panic(err)
	}
	consensusService := test.NewConsensusService(n)
	err = n.InitConsensus(consensusService)
	if err != nil {
		panic(err)
	}
	p2pService := &p2p.Network{}
	err = n.InitP2P(p2pService)
	if err != nil {
		panic(err)
	}
	return n, consensusService, p2pService, nodeAccount
}

func main() {
	configFile := "./config.json"

	config, err := config.NewConfig(configFile)
	if err != nil {
		panic(err)
	}
	if config == nil {
		panic("no config")
	}

	if len(config.GetSecret()) == 0 {
		panic("no secret")
	}
	n, consensusService, p2pService, nodeAccount := testConsensus(config)
	err = n.Start()
	if err != nil {
		panic(err)
	}

	blob := "670aa7010865121602002475880261f488a86cc3788af127009c46d162d2180220c0843d3243020004743275b498e96ac0e123d9bdbef11ebd839f3fd4bf9b728799b86ca1d63d5ab2bccbd4a6375c858f795585834354dd50deda0345c95b3d9e38557a4e467eaf673a40b2443b4eac2a882502aa13fdf41bb4056dd200cb852964bf1ac3b8cd74508f7e4b93cc59e7656e65e30fec760da10ee0bb47f25fa54328d68f7ecbcf9616529412a6100805228a01790a42087910051a1602002475880261f488a86cc3788af127009c46d162d220022a207d2f1164015f5f1be3d0400535211fc892123bff94886ff33c73090d5388366830014a43020004743275b498e96ac0e123d9bdbef11ebd839f3fd4bf9b728799b86ca1d63d5ab2bccbd4a6375c858f795585834354dd50deda0345c95b3d9e38557a4e467eaf672243790a40087910051a160200c287b1266732495fe8c93ce3ba631597153fdd912a2087b113f9c094c26d482c122f3f7cf7295aec44b2fcd05fbff058562798cd949330052a72910a012a126ca46464617461505e452700000000003600000000000000646e616d65784034384131384634434235454133333046423943304535303736314146423432453643413436344433463534354133323335423133433335323236423641314335647265667301666368756e6b73802a62910a012f125ca4646461746140646e616d65784034384131384634434235454133333046423943304535303736314146423432453643413436344433463534354133323335423133433335323236423641314335647265667301666368756e6b73802a63910a023533125ca4646461746140646e616d65784034384131384634434235454133333046423943304535303736314146423432453643413436344433463534354133323335423133433335323236423641314335647265667301666368756e6b73802a45910a201a6562590ef19d1045d06c4055742d38288e9e6dcd71ccde5cee80f1d5a774eb1220a46464617461423530646e616d6563676173647265667301666368756e6b73802aec05910a2048a18f4cb5ea330fb9c0e50761afb42e6ca464d3f545a3235b13c35226b6a1c512c605a46464617461590180a3636b6579582087744755614fda4d317a576469bb3a1cf11c6c032f9bf69973a901fb4aa1c1cb646e616d6565746f6b656e6567726f7570f5a3636b65795820d75fd5c44bb71004aa975eeabcdeb0cdfeae39ad002f0fae4fa7403a128ccdff646e616d6564636f64656567726f7570f5a3636b65795820ade71232335354f5052572d2bc7d8d14d77c458ac8fe97586dd67714e71cb4ba646e616d6568636f6e74726163746567726f7570f5a3636b65795820098e2d6e8c0d45b74cefebcfd03341bf1a06ebf1ad3ad42746a3ec4e4fab0dd3646e616d6564757365726567726f7570f5a3636b6579582067791b4b6aac06d0e71fc3b51165d7ac672dfa1d1bdd560c80b58c26a7c61229646e616d65782e6574682e3078633238374231323636373332343935466538633933434533426136333135393731353366646439316567726f7570f5a3636b65795820a54b1da2ec02d04594c0a38299e88374ee31b3bf3e737edb2054de215793c13e646e616d6564706167656567726f7570f5646e616d65612f647265667301666368756e6b7386a2636b6579582087744755614fda4d317a576469bb3a1cf11c6c032f9bf69973a901fb4aa1c1cb676e657874506f731839a2636b65795820d75fd5c44bb71004aa975eeabcdeb0cdfeae39ad002f0fae4fa7403a128ccdff676e657874506f731871a2636b65795820ade71232335354f5052572d2bc7d8d14d77c458ac8fe97586dd67714e71cb4ba676e657874506f7318ada2636b65795820098e2d6e8c0d45b74cefebcfd03341bf1a06ebf1ad3ad42746a3ec4e4fab0dd3676e657874506f7318e5a2636b6579582067791b4b6aac06d0e71fc3b51165d7ac672dfa1d1bdd560c80b58c26a7c61229676e657874506f73190148a2636b65795820a54b1da2ec02d04594c0a38299e88374ee31b3bf3e737edb2054de215793c13e676e657874506f731901802adb01910a204e7a559722d10bbb07ebc7f841099f20a4d822b8b243e1eec155d77ea5c6587312b501a464646174615863a3636b65795820bc1a5a9a25b00533f28f664b9836c3e31f224b4ec4901ac2877b846d9436b0df646e616d65782e6574682e3078633238374231323636373332343935466538633933434533426136333135393731353366646439316567726f7570f5646e616d65657374617465647265667301666368756e6b7381a2636b65795820bc1a5a9a25b00533f28f664b9836c3e31f224b4ec4901ac2877b846d9436b0df676e657874506f7318632adb01910a2067791b4b6aac06d0e71fc3b51165d7ac672dfa1d1bdd560c80b58c26a7c6122912b501a464646174615839a3636b657958204e7a559722d10bbb07ebc7f841099f20a4d822b8b243e1eec155d77ea5c65873646e616d656573746174656567726f7570f5646e616d65782e6574682e307863323837423132363637333234393546653863393343453342613633313539373135336664643931647265667301666368756e6b7381a2636b657958204e7a559722d10bbb07ebc7f841099f20a4d822b8b243e1eec155d77ea5c65873676e657874506f7318392ad901910a20bc1a5a9a25b00533f28f664b9836c3e31f224b4ec4901ac2877b846d9436b0df12b301a464646174615837a3636b657958201a6562590ef19d1045d06c4055742d38288e9e6dcd71ccde5cee80f1d5a774eb646e616d65636761736567726f7570f4646e616d65782e6574682e307863323837423132363637333234393546653863393343453342613633313539373135336664643931647265667301666368756e6b7381a2636b657958201a6562590ef19d1045d06c4055742d38288e9e6dcd71ccde5cee80f1d5a774eb676e657874506f7318372a45910a20d59eced1ded07f84c145592f65bdf854358e009c5cd705f5215bf18697fed1031220a46464617461423430646e616d6563676173647265667301666368756e6b738018aee08ba306"
	data, err := hex.DecodeString(blob)
	if err != nil {
		panic(err)
	}
	txWithData := &block.TransactionWithData{}
	err = txWithData.UnmarshalBinary(data)
	if err != nil {
		panic(err)
	}

	tx := txWithData.Transaction
	ok, err := consensusService.VerifyTransaction(nodeAccount, tx)
	fmt.Println("verify", ok, err)

	peers, err := p2pService.ListPeers(config.GetChainId())
	if err != nil {
		panic(err)
	}
	fmt.Println("peers", peers)
}
